package com.kgc.sbt.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.kgc.sbt.api.HotWordApi;
import com.kgc.sbt.util.RedisUtils;
import com.kgc.sbt.util.RequestResult;
import com.kgc.sbt.util.ResultBuildUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

/**
 * Created on 2021-06-22.
 * <p>
 * Author: a123
 * <p>
 * Description: 热词搜索操作入口
 */
@RestController
@Api(tags = "热词搜索操作入口，版本号：1.0.0")
public class HotWordController {

    @Reference
    private HotWordApi hotWordApi;

    @Autowired
    private RedisUtils redisUtils;


    /**
     * @return
     * @author a123
     * @date 2021-06-23
     * @description 获取排行榜前10的数据
     */
    @GetMapping("/selectTopByNumber")
    @ApiOperation(value = "获取排行榜前10的数据", notes = "查看排名前10的热词")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", required = true, value = "热词集合", defaultValue = "searchHotWord"),
    })
    @ApiResponses({
            @ApiResponse(code = 0003, message = "获取失败，无此数据")

    })
    public RequestResult<Set<Object>> selectTopByNumber(@RequestParam String key) {

        if (!redisUtils.top(key).isEmpty()) {
            return ResultBuildUtil.success(redisUtils.top(key));
        }
        return ResultBuildUtil.fail("0003", "获取失败，无此数据");
    }

    /**
     * @return
     * @author a123
     * @date 2021-06-23
     * @description 删除热词数据
     */
    @GetMapping("/delData")
    @ApiOperation(value = "删除某个热词数据", notes = "后台清理某个热词数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", required = true, value = "热词集合", defaultValue = "searchHotWord"),
            @ApiImplicitParam(name = "member", required = true, value = "热词名称")
    })
    @ApiResponses({
            @ApiResponse(code = 0004, message = "删除热词数据失败，无此数据")
    })
    public RequestResult<String> delData(@RequestParam String key, @RequestParam Object member) {

        if (redisUtils.remove(key, member) > 0) {
            return ResultBuildUtil.success("删除热词数据成功");
        }
        return ResultBuildUtil.fail("0004", "删除热词数据失败，无此数据");
    }


    /**
     * @author a123
     * @date 2021-06-25
     * @return
     * @description 获取排行榜start到end的value以及它的score
     */
    /*@GetMapping("/rangeWithScore")
    @ApiOperation(value = "获取排行榜start到end的value以及他的score",notes = " 查指定范围排名的数据")
    public RequestResult<Set<ZSetOperations.TypedTuple<Object>>> rangeWithScore(@RequestParam String key, @RequestParam long start,@RequestParam long end){

        if( redisUtils.rangeWithScore(key, start, end)!=null){
            return ResultBuildUtil.success(redisUtils.rangeWithScore(key, start, end));
        }
        return ResultBuildUtil.fail("0002","获取失败，无此数据");
    }*/
}
